package com.leiqichao.backend.handler;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理器
 * 使用 @ControllerAdvice 注解标记该类为全局异常处理器
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理资源未找到异常
     * 使用 @ExceptionHandler 注解指定处理的异常类型
     *
     * @param ex      捕获的异常对象
     * @param request 当前请求对象
     * @return 返回一个包含错误信息的 ResponseEntity
     */
    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
        // 创建一个 Map 来存储错误信息
        Map<String, Object> response = new HashMap<>();
        response.put("timestamp", new Date()); // 错误发生的时间
        response.put("status", HttpStatus.NOT_FOUND.value()); // HTTP 状态码
        response.put("error", "Not Found"); // 错误类型
        response.put("message", ex.getMessage()); // 异常信息
        response.put("path", request.getDescription(false)); // 请求路径
        return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); // 返回响应
    }

    /**
     * 处理无效请求异常
     *
     * @param ex      捕获的异常对象
     * @param request 当前请求对象
     * @return 返回一个包含错误信息的 ResponseEntity
     */
    @ExceptionHandler(InvalidRequestException.class)
    public ResponseEntity<?> handleInvalidRequestException(InvalidRequestException ex, WebRequest request) {
        Map<String, Object> response = new HashMap<>();
        response.put("timestamp", new Date());
        response.put("status", HttpStatus.BAD_REQUEST.value());
        response.put("error", "Bad Request");
        response.put("message", ex.getMessage());
        response.put("path", request.getDescription(false));
        return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
    }



    /**
     * 处理所有其他异常
     * 这是一个兜底的异常处理方法，用于处理未明确捕获的异常
     *
     * @param ex      捕获的异常对象
     * @param request 当前请求对象
     * @return 返回一个包含错误信息的 ResponseEntity
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<?> handleGlobalException(Exception ex, WebRequest request) {
        Map<String, Object> response = new HashMap<>();
        response.put("timestamp", new Date());
        response.put("status", HttpStatus.INTERNAL_SERVER_ERROR.value());
        response.put("error", "Internal Server Error");
        response.put("message", ex.getMessage());
        response.put("path", request.getDescription(false));
        return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 自定义异常：资源未找到异常
     * 当请求的资源不存在时抛出此异常
     */
    public class ResourceNotFoundException extends RuntimeException {
        public ResourceNotFoundException(String message) {
            super(message); // 调用父类构造函数，设置异常信息
        }
    }

    /**
     * 自定义异常：无效请求异常
     * 当客户端传递的参数无效时抛出此异常
     */
    public class InvalidRequestException extends RuntimeException {
        public InvalidRequestException(String message) {
            super(message); // 调用父类构造函数，设置异常信息
        }
    }
}